﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using OfficeOpenXml;

namespace EHR18
{
    public partial class FrmReport : BaseChild
    {
        public FrmReport()
        {
            InitializeComponent();
            this.dateTimePicker1.Value = DateTime.Today;
            this.dateTimePicker2.Value = DateTime.Today.AddHours(24);
        }
        private static FrmReport frm = null;
        TBAllService service;

        ModuleConfig module = GenneralConfig.instance.module;
        public static FrmReport getInstance()
        {
            if (frm == null || frm.IsDisposed)
            {
                frm = new FrmReport();
            }
            return frm;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            service = new TBAllService(dateTimePicker1.Value, dateTimePicker2.Value);
            var dt = service.GetTable(module.NameMappings);
            //   service.GetCPKDatas()
            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EditReportColumnName editReportColumnName = new EditReportColumnName();
            editReportColumnName.StartPosition = FormStartPosition.CenterScreen;
            editReportColumnName.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.DataSource != null)
            {
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.DefaultExt = "Xlsx";
                dialog.Filter = @"xlsx files(*.xlsx)| *.xlsx | xls files(*.xls) | *.xls | All files(*.*) | *.* ";
                if (dialog.ShowDialog() != DialogResult.OK)
                    return;
                string FileName = dialog.FileName;
                using (ExcelPackage excel = new ExcelPackage(new System.IO.FileInfo(FileName)))
                {
                    var ws = excel.Workbook.Worksheets.Add(System.IO.Path.GetFileNameWithoutExtension(FileName));
                    ws.Cells["A1"].LoadFromDataTable(dataGridView1.DataSource as DataTable, true);
                    excel.Save();
                }

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(service==null)
                service = new TBAllService(dateTimePicker1.Value, dateTimePicker2.Value);
            var listFp = new List<FPRate>();
            foreach (var a in module.TestItems.Where(t => t.IsUse == true))
            {
                List<string> list;
                if (a.Name == "图像")
                {
                    list= service.GetStrings(a.Name, "判定", "Result");
                }
                else
                {
                    list = service.GetStrings(a.Name, a.Name, "Result");
                }
                int pass = list.Where(t => t.ToUpper() == "PASS").Count();
                int fail = list.Where(t => t.ToUpper() == "FAIL").Count();
                listFp.Add(new FPRate() { Station = a.Name, Fail = fail, Pass = pass });
            }
            this.dataGridView1.DataSource = new BindingList<FPRate>(listFp);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (service == null)
                service = new TBAllService(dateTimePicker1.Value, dateTimePicker2.Value);
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.DefaultExt = "xlsx";
            saveFileDialog.Filter = @"xlsx files(*.xlsx)| *.xlsx | xls files(*.xls) | *.xls | All files(*.*) | *.* ";
            if (saveFileDialog.ShowDialog() != DialogResult.OK)
                return;
            string FileName = saveFileDialog.FileName;
            ExcelReport.Saveexcel(service,service.GetTable(GenneralConfig.instance.module.NameMappings),service.dtFrom,service.dtTo, AppDomain.CurrentDomain.BaseDirectory+"template.xlsx",FileName);
        }
    }

    public class FPRate
    {
        public string Station { get; set; }
        public int Pass { get; set; }
        public int Fail { get; set; }
        public int Total { get { return Pass + Fail; } }
        public string PassRate { get { return (Pass * 1.0 / Total).ToString("P"); } }
        public string FailRate { get { return (Fail * 1.0 / Total).ToString("P"); } }
    }
}
